<?php
namespace app\admin\controller;

use app\common\controller\AdminController;
use app\common\logic\AdminLogic;
use think\facade\Cache;

class AuthGroup extends AdminController
{
    public function initialize(){
        $this->modelName = 'AuthGroup';
        $this->controllerName = '人员类别';
        parent::initialize();
    }

    /**
     * 设置权限
     */
    public function access(){
        $alj = new AdminLogic($this->modelName);
        if(request()->isAjax()){
            $param = input('param.');
            if(empty($param['id'])){
                $this->error('id参数错误');
            }
            if(empty($param['rules'])){
                $rules = '';
            }
            else{
                $rules = $param['rules'];
            }
            $jieguo = $alj->_setAuthGroupRules($param['id'],$rules);
            if(0 === $jieguo['err_code']){
                Cache::clear('system');
                $this->success($jieguo['msg'],url('index'));
            }
            else{
                $this->error($jieguo['msg']);
            }
        }

        $id = input('id/d');
        if(empty($id)){
            $this->error('ID 出错');
        }
        $jieguo = $alj->getModelByPk($id);
        if($jieguo['err_code'] !== 0){
            $this->error($jieguo['msg']);
        }
        $agModel = $jieguo['data'];
        $arList = $alj->_getRuelsList('id,title,notcheck,pid');
        $this->assign('model',$agModel);
        $rules = explode(',',$agModel['rules']);

        // 勾选 本角色已经存在的权限
        $fanhui = [];
        foreach($arList as $key=>$vo){
            $fanhui[] = [
                'id' => $vo['id'],
                'pId' => $vo['pid'],
                'name' => $vo['title'],
                'checked' => (!empty($rules) && in_array($vo['id'], $rules)) ? 1 : 0,
            ];
        }

        $this->assign('arTreeJson',json_encode($fanhui,256));
        return $this->fetch('auth_group/access2');
    }
}
